package com.juma.cartridge.retryque;

public abstract class IntervalRetryHandler implements RetryHandler {

	protected Integer intervalSeconds = 60;
	
	private long taskLastTime = 0;
	
	public IntervalRetryHandler() {}
	
	public IntervalRetryHandler(int seconds) {
		this.intervalSeconds = seconds;
	}
	
	
	@Override
	public final RetryResult retry(RetryTask task) {
		//判断任务上一次执行与此时的时间间隔是否满足再试再银联查询
		long currentTime = System.currentTimeMillis();
		if(taskLastTime==0) {
			taskLastTime = task.getUpdateTime().getTime();
		}
		
		if ((currentTime-taskLastTime)<(intervalSeconds*1000)) {
			//如果时间间隔还未满,则返回,且任务hold住(保持)
			return RetryResult.RETRY;
		}
		RetryResult rr = doRetry(task);
		this.taskLastTime = currentTime;
		task.setRefresh(true);
		return rr;
	}

	protected abstract RetryResult doRetry(RetryTask task);

	public Integer getIntervalSeconds() {
		return intervalSeconds;
	}

	public void setIntervalSeconds(Integer intervalSeconds) {
		this.intervalSeconds = intervalSeconds;
	}
	
}
